/*
 * Copyright (c) 2020 Fraunhofer FOKUS and others. All rights reserved.
 *
 * See the NOTICE file(s) distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contact: mosaic@fokus.fraunhofer.de
 */

package org.eclipse.mosaic.interactions.mapping.advanced;

import static org.apache.commons.lang3.builder.ToStringStyle.SHORT_PREFIX_STYLE;

import org.eclipse.mosaic.lib.objects.trafficlight.TrafficLightGroup;
import org.eclipse.mosaic.rti.api.Interaction;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;

import java.util.Collection;
import java.util.Map;
import javax.annotation.concurrent.Immutable;

/**
 * The TrafficLight {@link Interaction} contains the phases and their duration of each traffic light in the simulation.
 * This interaction is generated by the traffic simulators at startup e.g. SumoAmbassador.
 */
@Immutable
public final class ScenarioTrafficLightRegistration extends Interaction {

    private static final long serialVersionUID = 1L;

    /**
     * String identifying the type of this interaction.
     */
    public final static String TYPE_ID = createTypeIdentifier(ScenarioTrafficLightRegistration.class);

    /**
     * Reference to the traffic lights in the simulation.
     */
    private final Collection<TrafficLightGroup> trafficLightGroups;

    private final Map<String, Collection<String>> lanesControlledByGroups;

    /**
     * Creates an interaction that informs that new traffic light groups have been added to the simulation by SUMO.
     *
     * @param time                   Timestamp of this interaction, unit: [ns]
     * @param trafficLightGroups     Reference to the traffic light groups in the simulation
     * @param lanesControlledByGroups Lanes controlled by the traffic light group (incoming lanes)
     */
    public ScenarioTrafficLightRegistration(long time,
                                            Collection<TrafficLightGroup> trafficLightGroups,
                                            Map<String, Collection<String>> lanesControlledByGroups) {
        super(time);
        this.trafficLightGroups = trafficLightGroups;
        this.lanesControlledByGroups = lanesControlledByGroups;
    }

    public Collection<TrafficLightGroup> getTrafficLightGroups() {
        return this.trafficLightGroups;
    }

    public Map<String, Collection<String>> getLanesControlledByGroups() {
        return lanesControlledByGroups;
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder(3, 41)
                .append(trafficLightGroups)
                .append(lanesControlledByGroups)
                .toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (obj == this) {
            return true;
        }
        if (obj.getClass() != getClass()) {
            return false;
        }

        ScenarioTrafficLightRegistration other = (ScenarioTrafficLightRegistration) obj;
        return new EqualsBuilder()
                .append(this.trafficLightGroups, other.trafficLightGroups)
                .append(this.lanesControlledByGroups, other.lanesControlledByGroups)
                .isEquals();
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this, SHORT_PREFIX_STYLE)
                .appendSuper(super.toString())
                .append("trafficLightGroups", trafficLightGroups)
                .append("lanesControlledByGroup", lanesControlledByGroups)
                .toString();
    }
}
